const oracledb = require('oracledb')

const xlsx = require('node-xlsx');
var fs = require('fs');

var moment = require('moment');

const DB_CONFIG = {
  user: 'cgxzcuser', 
  password: 'Tp3D1rBz#cx2024',
  //connectString: 'jdbc:oracle:thin:@172.50.100.181:1521:cgxzc' // 数据库地址：{IP:PORT/数据库名称}
  connectString: '172.50.100.181:1521/cgxzc' // 数据库地址：{IP:PORT/数据库名称}
}


console.log('运行模式:', oracledb.thin ? 'Thin' : 'Thick');

//oracledb.initOracleClient();
oracledb.initOracleClient({libDir: "C:\\Users\\15371\\scoop\\apps\\oracle-instant-client\\current"});



const 新产销 = `
select t1.*,t2.* from SO_ORDER@CGXZCDB_ZSJ t1  left join  SO_ORDER_dtl@CGXZCDB_ZSJ   t2 on t1.order_no=t2.order_no
where ROWNUM<10
`

const 新产销对比列 = `
SELECT
    t2.id,
    t1.order_no,--合同号
    t1.ORDER_NO_OLD,--老产销订单
    t2.ORDER_NO_OLD || '/' ||t2.ORDER_SEQ_OLD || '/' || t2.DELIVERY_NO_OLD AS order_item_num ,
    t1.CONTRACT_NO_CUST,--客户合同号
    t1.TRADE_TYPE,--贸易类型
    t1.CUSTOMER_NO,
    t1.STLMT_UNIT_NO,
    t2.PROD_TYPE_NO,
    case when (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_CLASSIFY' and t.STR_KEY=t1.ORDER_CLASSIFY) is not null then (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_CLASSIFY' and t.STR_KEY=t1.ORDER_CLASSIFY) else  t1.ORDER_CLASSIFY end ORDER_CLASSIFY,--分类
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_MODE' and t.STR_KEY=t1.ORDER_MODE  ) ORDER_MODE,--合同类型
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_TYPE' and t.STR_KEY=t1.ORDER_TYPE ) ORDER_TYPE,--分类--订货方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='saleIns' and t.STR_KEY=t1.SALE_INS ) SALE_INS,--销售组织
    (select  STR_NAME from hr_department@CGXZCDB_ZSJ t where STR_SN=SALE_ORG) SALE_ORG,--片区
-- 		ORDER_YEAR,--合同年度
    case when  (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.SALESMAN) is not null then (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.SALESMAN) else  t1.SALESMAN end SALESMAN,--业务员
    case when  (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.DOCUMENTARY_NAME) is not null then (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.DOCUMENTARY_NAME) else  t1.DOCUMENTARY_NAME end DOCUMENTARY_NAME,--跟单人
    t1.SIGN_DATE,--签订日期
    case when t1.BUSINESS_TYPE='1' then '出口' else '非出口' end BUSINESS_TYPE,--业务分类
    t1.SIGN_ADDRESS,--签订地点
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='saleOrg' and t.STR_KEY=t1.COMPANY_ID) COMPANY_ID,--卖出单位
    t1.CUSTOMER_NM,--订货单位
    t1.STLMT_UNIT_NM,--结算单位
    case when t1.PRODUCE_PLACE='Z' then '自产' else '外购' end PRODUCE_PLACE,--产地区分
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='SUPPLY_UNIT' and t.STR_KEY=t1.SUPPLY_UNIT ) SUPPLY_UNIT,--制造商
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='SETTLE_FREIGHT_MODE' and t.STR_KEY=t1.FREIGHT_MODE )  FREIGHT_MODE,--运费开票方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_INVOICE_TYPE' and t.STR_KEY=t1.INVOICE_TYPE ) INVOICE_TYPE,--发票类型
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='saleBillDate' and t.STR_KEY=t1.BILL_DATE ) BILL_DATE,
    t1.BANK,--开户行
    t1.ACCOUNT,--开户行账号
    t1.TAX_ID,--税号
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='PRICE_FL' and t.STR_KEY=t1.PRICE_FL) PRICE_FL,--定价方式
-- 		CONTAIN_TAX,--是否含税
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='currency' and t.STR_KEY=t1.CURRENCY)  CURRENCY,--币种
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='MNY_ASK_TYP' and t.STR_KEY=t1.MNY_ASK_TYP)  MNY_ASK_TYP,--付款方式
    t1.PRICE_PLAN,--优惠方案
-- 		MNY_ASK_DESC,--付款要求
    t1.LOCAL_LAW,--当地法律法规
    '',--其他信息
    t1.ORDER_REQUIRE_STATUS,--订单要求
    t1.PORT_OF_DESTINATION,--目的港
-- 		'',--罚则
-- 		PENALTY_DESC--罚则条款
    t2.ORDER_SEQ,--合同行
    t2.ORDER_SEQ_OLD,--老产销合同行
    t2.DELIVERY_NO_OLD,--老产销交货行
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='orderDtlStatus' and t.STR_KEY=t2.ORDER_STATUS )  ORDER_STATUS,--状态
    t2.MONTH,--排产月份
    t2.GROUP_ORDER_NO||'/'||GROUP_ORDER_SEQ GROUP_ORDER_NO, --集团订单号
    t2.PROD_SPEC_CODE,--规范码
    t2.PROD_SPEC,--产品规范描述
    t2.APN_CODE,--用途码代码
    t2.APN_DEF,--用途码描述
    t2.PROD_NAME,--规范品名
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='qualityLevels' and t.STR_VALUE2=t2.QLY_GRADE_NO  ) QLY_GRADE_NO,--质量等级
    t2.TRADE_NO,--规范钢级
    t2.PROD_STD,--规范标准
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='pipeType' and t.STR_KEY=t2.PIPE_TYPE  )  PIPE_TYPE,--管端类型
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='manuMethod' and t.STR_KEY=t2.MANU_METHOD  ) MANU_METHOD,--制造方式
    t2.DELIVERY_STATUS_NAME,--规范交货状态
    t2.SALE_PROD_CODE_NAME,--订货品名
    t2.SALE_SG_STD_NM,--订货标准
    t2.SALE_SHOP_SIGN_NAME,--订货钢级
    t2.SALE_GOOD_STATUS_NAME,--订货交货状态
    t2.OUT_DIA,--外径
    t2.WAL_THK,--壁厚
    t2.MODEL_NAME,--扣型
    t2.SPEC_CODE_NAME,--产品规格
    t2.ORDER_SPEC_CODE_NAME,--订货规格
    t2.WGT,--订货量
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='orderUnit' and t.STR_KEY=t2.UNIT  ) UNIT,--订货单位
    t2.LEN_MIN,--长度下限
    t2.LEN_MAX,--长度上限
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='LEN_UNIT' and t.STR_KEY=t2.LEN_UNIT  ) LEN_UNIT,--长度单位
    t2.ORD_ENSU_TOTL_LEN,--保总长
    t2.SHRT_LEN_RATO,--短尺率
    t2.SHORTEST,--短尺最短
    t2.SHRT_STOVE,--短尺随炉
    t2.AVG_LEN_MIN,--平均长度大于
    t2.AVG_LEN_MAX,--平均长度小于
    t2.DELVRY_RANGE_MIN,--交货量差下限
    t2.DELVRY_RANGE_MAX,--交货量差上限
    t2.SIGL_LEN,--单倍长度
    t2.DELVRY_EDATE,--交货期
    t2.CONSIGNEE_COM,--收货单位
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='TRANS_TYPE' and t.STR_KEY=t2.TRANS_TYPE  ) TRANS_TYPE,--运输方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='transMode' and t.STR_KEY=t2.TRANS_MODE  ) TRANS_MODE,--运输模式
    t2.STATION_NAME,--到站
    t2.SPCL_LN_NM,--专用线
    t2.MEMO,--备注
    t2.FINAL_USER_NM,--最终用户
    t2.FINAL_USER,
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='so_order_qualityUser' and t.STR_KEY=t2.QUALITY_USER  ) QUALITY_USER,--质保书单位
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='OUTBOUND_CALCULATE' and t.STR_KEY=t2.OUTBOUND_CALCULATE  ) OUTBOUND_CALCULATE,--出库计量
    case when OUTS_BILLET_FL='1' then '外购' else '自炼' end OUTS_BILLET_FL,--外购坯
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='order_dtl_outs_type' and t.STR_KEY=t2.OUTS_PIPE  ) OUTS_PIPE,--外购管
    case when MONITOR_FLAG='1' then '监制' else '不监制' end MONITOR_FLAG,--是否监制
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='MAN_INSPECTION' and t.STR_KEY=t2.MAN_INSPECTION  ) MAN_INSPECTION,--在线机探盲区
    t2.PROJECT_NO,--订单工程编号
    case when COOPERATE_FLAG='1' then '是' else '否' end COOPERATE_FLAG,--是否协同
    t2.EXTRA_REQUIRE_OLD,--合同附加要求
    t2.PRO_TEC_REQUIRE_OLD,--产品技术要求
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='PROTECT_TYPE' and t.STR_KEY=t2.KEEP_TYPE  ) KEEP_TYPE,--保价类型
    t2.FREIGHT,--运费
    t2.OUT_PRICE,--出厂价
    t2.PAK_PRICE,--吊装费
    t2.BALNCE_PRICE--结算价
from SO_ORDER@CGXZCDB_ZSJ t1  left join  SO_ORDER_dtl@CGXZCDB_ZSJ   t2 on t1.order_no=t2.order_no  where  t1.order_status<>'XX' and t2.order_status<>'XX'  and t1.order_type<>'X'
 and ROWNUM<10
`


const 新产销对比列去除非caseWhen = `
SELECT

    case when (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_CLASSIFY' and t.STR_KEY=t1.ORDER_CLASSIFY) is not null then (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_CLASSIFY' and t.STR_KEY=t1.ORDER_CLASSIFY) else  t1.ORDER_CLASSIFY end ORDER_CLASSIFY,--分类
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_MODE' and t.STR_KEY=t1.ORDER_MODE  ) ORDER_MODE,--合同类型
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_TYPE' and t.STR_KEY=t1.ORDER_TYPE ) ORDER_TYPE,--分类--订货方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='saleIns' and t.STR_KEY=t1.SALE_INS ) SALE_INS,--销售组织
    (select  STR_NAME from hr_department@CGXZCDB_ZSJ t where STR_SN=SALE_ORG) SALE_ORG,--片区
-- 		ORDER_YEAR,--合同年度
    case when  (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.SALESMAN) is not null then (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.SALESMAN) else  t1.SALESMAN end SALESMAN,--业务员
    case when  (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.DOCUMENTARY_NAME) is not null then (select  NAME from HR_USER@CGXZCDB_ZSJ where USER_NO=t1.DOCUMENTARY_NAME) else  t1.DOCUMENTARY_NAME end DOCUMENTARY_NAME,--跟单人
    case when t1.BUSINESS_TYPE='1' then '出口' else '非出口' end BUSINESS_TYPE,--业务分类
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='saleOrg' and t.STR_KEY=t1.COMPANY_ID) COMPANY_ID,--卖出单位
    case when t1.PRODUCE_PLACE='Z' then '自产' else '外购' end PRODUCE_PLACE,--产地区分
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='SUPPLY_UNIT' and t.STR_KEY=t1.SUPPLY_UNIT ) SUPPLY_UNIT,--制造商
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='SETTLE_FREIGHT_MODE' and t.STR_KEY=t1.FREIGHT_MODE )  FREIGHT_MODE,--运费开票方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='ORDER_INVOICE_TYPE' and t.STR_KEY=t1.INVOICE_TYPE ) INVOICE_TYPE,--发票类型
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='saleBillDate' and t.STR_KEY=t1.BILL_DATE ) BILL_DATE,
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='PRICE_FL' and t.STR_KEY=t1.PRICE_FL) PRICE_FL,--定价方式
-- 		CONTAIN_TAX,--是否含税
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='currency' and t.STR_KEY=t1.CURRENCY)  CURRENCY,--币种
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='MNY_ASK_TYP' and t.STR_KEY=t1.MNY_ASK_TYP)  MNY_ASK_TYP,--付款方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='orderDtlStatus' and t.STR_KEY=t2.ORDER_STATUS )  ORDER_STATUS,--状态

    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='qualityLevels' and t.STR_VALUE2=t2.QLY_GRADE_NO  ) QLY_GRADE_NO,--质量等级
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='pipeType' and t.STR_KEY=t2.PIPE_TYPE  )  PIPE_TYPE,--管端类型
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='manuMethod' and t.STR_KEY=t2.MANU_METHOD  ) MANU_METHOD,--制造方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='orderUnit' and t.STR_KEY=t2.UNIT  ) UNIT,--订货单位
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='LEN_UNIT' and t.STR_KEY=t2.LEN_UNIT  ) LEN_UNIT,--长度单位

    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='TRANS_TYPE' and t.STR_KEY=t2.TRANS_TYPE  ) TRANS_TYPE,--运输方式
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='transMode' and t.STR_KEY=t2.TRANS_MODE  ) TRANS_MODE,--运输模式

    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='so_order_qualityUser' and t.STR_KEY=t2.QUALITY_USER  ) QUALITY_USER,--质保书单位
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='OUTBOUND_CALCULATE' and t.STR_KEY=t2.OUTBOUND_CALCULATE  ) OUTBOUND_CALCULATE,--出库计量
    case when OUTS_BILLET_FL='1' then '外购' else '自炼' end OUTS_BILLET_FL,--外购坯
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='order_dtl_outs_type' and t.STR_KEY=t2.OUTS_PIPE  ) OUTS_PIPE,--外购管
    case when MONITOR_FLAG='1' then '监制' else '不监制' end MONITOR_FLAG,--是否监制
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='MAN_INSPECTION' and t.STR_KEY=t2.MAN_INSPECTION  ) MAN_INSPECTION,--在线机探盲区
    case when COOPERATE_FLAG='1' then '是' else '否' end COOPERATE_FLAG,--是否协同
    (select  STR_VALUE from SYS_DICTIONARY_DTL@CGXZCDB_ZSJ  t where t.DICT_SN='PROTECT_TYPE' and t.STR_KEY=t2.KEEP_TYPE  ) KEEP_TYPE --保价类型
from SO_ORDER@CGXZCDB_ZSJ t1  left join  SO_ORDER_dtl@CGXZCDB_ZSJ   t2 on t1.order_no=t2.order_no  where  t1.order_status<>'XX' and t2.order_status<>'XX'  and t1.order_type<>'X'
                                                                                                     and ROWNUM<10
`



  const date = moment().format('YYYY-MM-DD HH-mm');
  const dateSubFix = `99999-${date}`;
// 使用连接池
async function run() {
  let pool;
  try {
    pool = await oracledb.createPool(DB_CONFIG);
    let connection;
    try {
      connection = await pool.getConnection();
    //   connection.execute('select * from AA_AARF ',(err,result)=>{
    //     console.log('result:',result)
    //   })
     // const 新产销与老产销Result = await 


      const 新产销data = await connection.execute(新产销);
      const ncx = 新产销data.metaData.map(s=>s.name);

      //const 新产销对比列Data =await connection.execute(新产销对比列)

      const 新产销对比列Data =await connection.execute(新产销对比列去除非caseWhen)


      //新产销对比列去除非caseWhen


      const ncxCompare = 新产销对比列Data.metaData.map(s=>s.name);
      const dif = ncx.filter(item => !ncxCompare.includes(item));
      console.log('新产销总列与新产销对比列的差集:',dif.length,dif)
      const data = [];
       const needFlag = []
      for(var item of new Set(dif)){
        const sqlGroupBy =  `
       select count(m.${item}),m.${item} as "columnKey" from (
        select t1.*,t2.* from SO_ORDER@CGXZCDB_ZSJ t1  left join  SO_ORDER_dtl@CGXZCDB_ZSJ   t2 on t1.order_no=t2.order_no
       ) m group by m.${item}
        `

      //     const sqlNull =  `
      //  select count(m.${item}) as "columnKey" from (
      //   select t1.*,t2.* from SO_ORDER@CGXZCDB_ZSJ t1  left join  SO_ORDER_dtl@CGXZCDB_ZSJ   t2 on t1.order_no=t2.order_no
      //  ) m where m.${item} is null or m.${item}=''
      //   `

         const sqlNull = (tableName)=> `
       select count(1) as "columnKey" from ${tableName} m where m.${item} is null or m.${item}=''   
        `

       

        try{
          let result = await connection.execute(sqlNull('SO_ORDER@CGXZCDB_ZSJ'))
           console.log(`result:${item};${sqlNull('SO_ORDER@CGXZCDB_ZSJ')}`,result.rows)
           if(result.rows[0][0]==0){
              needFlag.push(
                {
                  column:'SO_ORDER@CGXZCDB_ZSJ.'+item,
                  size:result.rows[0][0]
                }
              )
           }
            result = await connection.execute(sqlNull('SO_ORDER_dtl@CGXZCDB_ZSJ'))
           console.log(`result:${item};${sqlNull('SO_ORDER_dtl@CGXZCDB_ZSJ')}`,result.rows)
            if(result.rows[0][0]==0){
              needFlag.push(
                {
                  column:'SO_ORDER_dtl@CGXZCDB_ZSJ.'+item,
                  size:result.rows[0][0]
                }
              )
           }
        }catch(e){

        }

       
       
      }

       console.log('needFlag:',needFlag)

      //   var buffer = xlsx.build([
      //       {name:'统计',data:[result.metaData.map(s=>s.name),...result.rows]},
      // ]);
      //   fs.writeFileSync(`./${name}.xlsx`,buffer,{'flag':'w'});

    

      


     
    //   connection.execute(新产销与老产销+ ` where S1.PROD_SPEC_CODE = '99999'`,(err,result)=>{
    //          const name = `新产销与老产销-${dateSubFix}`
    //         var buffer = xlsx.build([
    //              {name:'统计',data:[result.metaData.map(s=>s.name),...result.rows]},
    //         ]);
    //          fs.writeFileSync(`./${name}.xlsx`,buffer,{'flag':'w'});
    //   });

    } catch (err) {
      throw (err);
    } finally {
      if (connection) {
        try {
          await connection.close(); 
        } catch (err) {
          throw (err);
        }
      }
    }
  } catch (err) {
    console.log(err)
    throw err;
  } finally {
    await pool.close();
  }
}
run()